package com.bangdream.michelia.presenter;

import android.app.Dialog;

import com.alibaba.fastjson.JSON;
import com.bangdream.michelia.contract.CollectionContract;
import com.bangdream.michelia.entity.BaseEntity;
import com.bangdream.michelia.entity.CollectionBean;
import com.bangdream.michelia.entity.CollectionRecordBean;
import com.bangdream.michelia.entity.LiveBean;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.model.ICollectionModelImpl;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.rejava.BaseObserverEntityNormalHttp;
import com.bangdream.michelia.tool.rejava.BaseObserverStringNormalHttp;
import com.bangdream.michelia.tool.retrofithttp.TokenManagerDef;
import com.bangdream.michelia.utils.L;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date on 2018/8/12 21:36
 * @email 909732184@qq.com
 */
public class CollectionPresenter<V> extends BasePresenter<V> {
    private static final String TAG = "CollectionPresenter";
    private CollectionContract.ICollectionModel model;

    public CollectionPresenter() {
        this.model = new ICollectionModelImpl();
    }
    public void getCollectionList(final int pageNo, int pageSize, String userId, Dialog dialog){
        if(getMyView()==null)
            return;
        Map paramsMap = new HashMap();
        setPd(dialog);
        paramsMap.put("pageNo",pageNo);
        paramsMap.put("pageSize",pageSize);
        paramsMap.put("userId",userId);
        paramsMap.put("delFlag","0");
        TokenManagerDef.work(() -> model.getCollectionList(paramsMap)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<CollectionBean>>(this,pd) {
                    @Override
                    public void onHandleSuccess(List<CollectionBean> beans) {
                        if(getMyView()==null)
                            return;
                        getMyView().setCollectionList(true,pageNo,beans,getDescribe(beans));
                    }
                });
    }

    public void actionCollection(String courseId,String userId, Dialog dialog){
        if(getMyView()==null)
            return;
        Map paramsMap = new HashMap();
        setPd(dialog);
        paramsMap.put("courseId",courseId);
        paramsMap.put("userId",userId);
        TokenManagerDef.work(() -> model.actionCollection(paramsMap)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<CollectionRecordBean>(this,pd) {
                    @Override
                    public void onHandleSuccess(CollectionRecordBean beans) {
                        if(getMyView()==null)
                            return;
                        if(beans!=null) {
                            getMyView().actionCollection(true, beans, "请求成功");
                        }else {
                            getMyView().actionCollection(false, beans, "收藏失败成功");
                        }
                    }
                });
    }

    public void actionCencelCollection(String id,Dialog pd){
        if(getMyView()==null)
            return;
        L.d(id);
        setPd(pd);
        TokenManagerDef.work(() -> model.actionCencelCollection(id)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<BaseEntity>(this,pd) {
                    @Override
                    public void onHandleSuccess(BaseEntity beans) {
                        if(getMyView()==null)
                            return;
//                        L.d(beans);
//                        if(beans!=null&&beans.isOk()) {
                            getMyView().actionCencelCollection(true,  "取消收藏成功");
//                        }else {
//                            getMyView().actionCencelCollection(false,  "取消收藏失败");
//                        }
                    }

                });
    }

    private CollectionContract.ICollectionView getMyView(){
            return (CollectionContract.ICollectionView) obtainView();
    }
}
